/**
 * Filename: StatusDetialAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/13 11:02
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.UserStatusActivity;
import com.github.postsanf.yinian.bean.YNComment;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.SnackBarUtils;
import com.github.postsanf.yinian.utils.TimeUtils;
import com.github.postsanf.yinian.utils.ToastUtils;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;

import java.util.List;

public class StatusDetialAdapter extends RecyclerView.Adapter<StatusDetialAdapter.ViewHolder>{
	public static final int TYPE_HEADER = 0;
	public static final int TYPE_NORMAL = 1;
	private List<YNComment> mDatas;
	private View mHeaderView;
	private Context	mContext;
	private ImageLoader	mImageLoader;
	private int groupid;
	private OnItemClickListener mListener;

	public void setOnItemClickListener(OnItemClickListener listener)
	{
		this.mListener = listener;
	}

	public StatusDetialAdapter(Context mContext,List<YNComment> mDatas,int groupid)
	{
		this.mDatas = mDatas;
		this.mContext = mContext;
		this.mImageLoader = ImageLoader.getInstance();
		this.groupid = groupid;
	}

	public  class ViewHolder extends RecyclerView.ViewHolder {
		LinearLayout	ll_comment;
		SimpleDraweeView comment_user_avatar;
		TextView		comment_user_name;
		TextView 		comment_time;
		TextView		comment_content;

		public ViewHolder(View itemView) {
			super(itemView);
			if(itemView == mHeaderView) return;

			ll_comment = (LinearLayout) itemView
					.findViewById(R.id.ll_comment);
			comment_user_avatar = (SimpleDraweeView) itemView
					.findViewById(R.id.comment_user_avatar);
			comment_user_name = (TextView) itemView
					.findViewById(R.id.comment_user_name);
			comment_time = (TextView) itemView
						.findViewById(R.id.comment_time);
			comment_content = (TextView) itemView
						.findViewById(R.id.comment_content);
		}

		public void bindData(final YNComment listItems)
		{
			if (listItems != null)
			{
				String toSb = "";
				if (listItems.getCommentedUser().getUserid()!=CommonConstants.SYSTEM_ID)
				{
					toSb = "@"+listItems.getCommentedUser().getUnickname()+": ";
				}

				String avatarPaths = YNUrlHelper.getAvatarUrl(mContext,listItems.getCommentUser().getUpic());
				Uri avatarUri = Uri.parse(avatarPaths);
				ImageOptHelper.showFrescoAvatar(avatarUri, comment_user_avatar, mContext);

				comment_user_name.setText(listItems.getCommentUser().getUnickname());
				comment_time.setText(TimeUtils.getGroupStatusTime(listItems.getCtime()));
				comment_content.setText(toSb+listItems.getCcontent());


				// 点击用户头像或昵称跳转
				comment_user_avatar.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						lookUser(comment_user_avatar,listItems);
					}
				});

				ll_comment.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if(mListener != null)
						{
							mListener.OnItemClick(v, listItems);
						}
					}
				});
			}
		}
	}

	private void lookUser(View view,YNComment item){
		if (groupid>0){
			Intent intent = new Intent(mContext, UserStatusActivity.class);
			intent.putExtra(CommonConstants.YNUSERID,item.getCommentUser().getUserid());
			intent.putExtra(CommonConstants.YNGROUPID, groupid);
			mContext.startActivity(intent);
		}else{
//			ToastUtils.showToast(mContext, "个人页面无法查看评论用户详情", Toast.LENGTH_SHORT);
			SnackBarUtils.makeShort(view,"个人页面无法查看评论用户详情").show();
		}
	}

	public void setHeaderView(View headerView) {
		mHeaderView = headerView;
		notifyItemInserted(0);
	}
	public View getHeaderView() {
		return mHeaderView;
	}

	@Override
	public int getItemViewType(int position) {
		if(mHeaderView == null) return TYPE_NORMAL;
		if(position == 0) return TYPE_HEADER;
		return TYPE_NORMAL;
	}

	private YNComment getItem(int position) {
		return mDatas.get(position);
	}

	// 创建ViewHolder
	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		if(mHeaderView != null && viewType == TYPE_HEADER) return new ViewHolder(mHeaderView);
		final View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_comment , parent, false);
		return new ViewHolder(convertView);
	}

	// 绑定数据到View
	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		if(getItemViewType(position) == TYPE_HEADER) return;
		final int pos = getRealPosition(holder);
		YNComment listItems = getItem(pos);
		holder.bindData(listItems);
		holder.itemView.setTag(listItems);
	}

	public interface OnItemClickListener
	{
		public void OnItemClick(View view,YNComment data);
	}


	public int getRealPosition(RecyclerView.ViewHolder holder) {
		int position = holder.getLayoutPosition();
		return mHeaderView == null ? position : position - 1;
	}

	@Override
	public int getItemCount() {
		return mHeaderView == null ? mDatas.size():mDatas.size() + 1 ;
	}
}
